package logic

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"iot-base/auth/model"
	"iot-base/auth/svc"
	types2 "iot-base/auth/types"
	"strings"
)

type TaskLogic struct {
}

func NewTaskLogic() *TaskLogic {
	return &TaskLogic{}
}

func (l *TaskLogic) AddTask(data types2.ReqAddTask, rule []byte) (model.Tasks, error) {
	task := model.Tasks{
		Day:       data.Day,
		TaskName:  data.TaskName,
		TaskType:  data.TaskType,
		StationId: data.StationId,
		ModelId:   data.ModelId,
		TimeType:  strings.ReplaceAll(data.TimeType, " ", ""),
		Switch:    data.Switch,
		Rule:      nil,
		Level:     0,
		ExecTime:  data.ExecTime,
	}
	if rule != nil {
		_ = task.Rule.Scan(rule)
	}
	err := svc.Dao.TTasks.Add(&task)
	if err != nil {
		return task, err
	}
	return task, nil
}

func (l *TaskLogic) ModifyTaskById(data types2.ReqModifyTask, rule []byte) (model.Tasks, error) {
	task := model.Tasks{
		Day:       data.Day,
		TaskName:  data.TaskName,
		TaskType:  data.TaskType,
		StationId: data.StationId,
		ModelId:   data.ModelId,
		Switch:    data.Switch,
		TimeType:  strings.ReplaceAll(data.TimeType, " ", ""),
		Rule:      nil,
		Level:     0,
		ExecTime:  data.ExecTime,
	}
	task.ID = data.Id
	if rule != nil {
		_ = task.Rule.Scan(rule)
	}
	err := svc.Dao.TTasks.Save(&task)
	if err != nil {
		return task, err
	}
	return task, nil
}

func (l *TaskLogic) FindTaskByTimeType(timeType string, page, size int) ([]types2.RespTaskList, error) {
	resp, err := svc.Dao.TTasks.FindTaskByTimeType(timeType, page, size)
	if err != nil {
		return nil, err
	}
	var result = make([]types2.RespTaskList, 0, len(resp))
	for index := range resp {
		var dataJson []types2.CaliParams
		param := types2.CaliParams{}
		_ = json.Unmarshal([]byte(resp[index].Rule.String()), &dataJson)
		if len(dataJson) > 0 {
			param = dataJson[0]
		}
		temp := types2.RespTaskList{
			Id:        resp[index].ID,
			StationId: resp[index].StationId,
			ModelId:   resp[index].ModelId,
			TaskName:  resp[index].TaskName,
			TaskType:  resp[index].TaskType,
			TimeType:  strings.ReplaceAll(resp[index].TimeType, " ", ""),
			Day:       resp[index].Day,
			Number:    int(param.Number),
			Ext:       param.Ext,
			ExecTime:  resp[index].ExecTime,
			Flow:      param.Flow,
			Switch:    resp[index].Switch,
		}
		result = append(result, temp)
	}
	return append([]types2.RespTaskList{}, result...), err
}

func (l *TaskLogic) FindTaskById(id int64) (model.Tasks, error) {
	return svc.Dao.TTasks.FindById(id)
}

func (l *TaskLogic) DelTaskById(id int64) error {
	return svc.Dao.TTasks.Delete(id)
}

func (l *TaskLogic) FindLog(stationId string, page, size int, planType string) (gin.H, error) {
	total, err := svc.Dao.TTaskLog.FindLogCount(stationId, planType)
	if err != nil {
		return nil, err
	}
	list, err := svc.Dao.TTaskLog.FindLogList(stationId, page, size, planType)
	if err != nil {
		return nil, err
	}
	if len(list) > 0 {
		pids := make([]string, 0, 3)
		for i := range list {
			if list[i].TaskType == "ZeroDriftAndSpanDrift" {
				pids = append(pids, list[i].ID)
			}
		}
		if len(pids) > 0 {
			subList, _ := svc.Dao.TTaskLog.QuerySubTaskLog(pids)
			if len(subList) > 0 {
				list = append(list, subList...)
			}
		}

	}
	return gin.H{"total": total, "list": list}, nil
}

func (l *TaskLogic) FindLogDetail(id string) ([]model.TaskLogDetails, error) {
	return svc.Dao.TTaskLogDetail.FindByTaskID(id)
}
